<?php

namespace App\Admin\Controllers\news;

use App\Common\statement\CommonStatement;
use App\Models\news\News;
use App\Models\news\Notice;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class NoticeController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '公告';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Notice());

        $grid->model()->latest();
        $grid->disableExport();
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
        });

        $grid->column('id',__('ID'));
        $grid->column('title', __('Title'));

        $grid->column('status',__('Status'))->display(function ($status){
            return CommonStatement::allow_status[$status];
        });;
        $grid->column('publish_time',__('Publish Time'));
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Notice::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('cate_id', __('Cate'));
        $show->field('content', __('Content'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Notice());
        $form->text('title',__('Title'))->required();
        $form->text('content');
        $form->radio('recommend',__('Recommend'))->options([
            '1' => '是',
            '0' => '否'
        ])->default(1);
        $form->radio('status',__('Status'))->options(CommonStatement::allow_status)->default(1);
        $form->saving(function (Form $form) {
            if($form->status == 1){
                $form->model()->publish_time = date("Y-m-d H:i:s");
            }
        });
        return $form;
    }
}
